package com.company.genealogy.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.genealogy.common.result.ApiResponse;
import com.company.genealogy.common.util.JwtUtil;
import com.company.genealogy.web.entity.Menu;
import com.company.genealogy.web.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.company.genealogy.common.constant.ApiCodeEnum;

@RestController
@RequestMapping("/api/v1/menus")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private JwtUtil jwtUtil;

    // 菜单列表
    @GetMapping
    public ApiResponse<IPage<Menu>> list(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int size,
                                       @RequestParam(required = false) String name,
                                       @RequestParam(required = false) Integer status,
                                       @RequestParam(required = false) Long parentId) {
        Page<Menu> p = new Page<>(page, size);
        
        // 构建查询条件
        com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Menu> queryWrapper = new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>();
        
        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like("name", name.trim());
        }
        
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        
        if (parentId != null) {
            queryWrapper.eq("parent_id", parentId);
        }
        
        IPage<Menu> result = menuService.page(p, queryWrapper);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, result);
    }

    // 获取所有启用的菜单
    @GetMapping("/enabled")
    public ApiResponse<List<Menu>> getEnabledMenus() {
        List<Menu> menus = menuService.getEnabledMenus();
        return ApiResponse.success(ApiCodeEnum.SUCCESS, menus);
    }

    // 根据角色ID获取菜单
    @GetMapping("/by-role/{roleId}")
    public ApiResponse<List<Menu>> getMenusByRole(@PathVariable Long roleId) {
        List<Menu> menus = menuService.getMenusByRoleId(roleId);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, menus);
    }

    // 根据用户ID获取菜单
    @GetMapping("/by-user/{userId}")
    public ApiResponse<List<Menu>> getMenusByUser(@PathVariable Long userId) {
        List<Menu> menus = menuService.getMenusByUserId(userId);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, menus);
    }

    // 获取当前用户有权限的菜单树
    @GetMapping("/user/tree")
    public ApiResponse<List<Menu>> getUserMenuTree(@RequestHeader("Authorization") String fromToken)  {
        String usernameFromToken = jwtUtil.getUsernameFromToken(fromToken);
        List<Menu> menuTree = menuService.getMenuTreeByUsername(usernameFromToken);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, menuTree);
    }

    // 新增菜单
    @PostMapping
    public ApiResponse<Menu> create(@RequestBody Menu menu) {
        menuService.save(menu);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, menu);
    }

    // 菜单详情
    @GetMapping("/{id}")
    public ApiResponse<Menu> detail(@PathVariable Long id) {
        Menu menu = menuService.getById(id);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, menu);
    }

    // 编辑菜单
    @PutMapping("/{id}")
    public ApiResponse<Void> update(@PathVariable Long id, @RequestBody Menu menu) {
        menu.setId(id);
        menuService.updateById(menu);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }

    // 删除菜单
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        menuService.removeById(id);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }
} 